package net.magicalcloud.magicalcloud.presenter;

import net.magicalcloud.magicalcloud.AppApplication;
import net.magicalcloud.magicalcloud.base.BasePresenter;
import net.magicalcloud.magicalcloud.entity.Files;
import net.magicalcloud.magicalcloud.execption.CloudExecption;
import net.magicalcloud.magicalcloud.listener.CloudListener;
import net.magicalcloud.magicalcloud.model.IFileModel;
import net.magicalcloud.magicalcloud.model.modelImpl.FileModel;
import net.magicalcloud.magicalcloud.view.ICloudView;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/** 文件presenter
 * Created by Pei on 2018/6/7.
 */

public class CloudPresenter extends BasePresenter<ICloudView> {

    public final int TYPE_BACK=0;
    public final int TYPE_REFRESH=1;
    public final int TYPE_FORWARD=2;

    private IFileModel fileModel=new FileModel();

    //文件id栈
    private Stack<String> fileIdStack =new Stack<>();
    //子目录栈
    private Stack<List<Files>> subFileStack =new Stack<>();



    /**
     *  请求子文件
     * @param type 返回上一个目录为0，刷新 为 1 前往下一个目录为 2
     * @param fileId 如果type为2，那么fileId为我前往的那个文件夹，也就是我需要查找这个文件夹下面有哪些文件
     */
    public void requestSubfile(final int type, String fileId){



        //返回上一层目录
        if(type==TYPE_BACK){
            if(subFileStack.empty()||fileIdStack.empty()){
                return;
            }
            //栈顶元素并弹出
            subFileStack.pop();
            fileIdStack.pop();
            List<Files>files=subFileStack.peek();
            getView().setRecycleViewData(files);
        }

        else {
            //前往下一个目录
            if(type==TYPE_FORWARD){
                if(fileId==null||fileId.equals("")){
                    fileId= String.valueOf(AppApplication.getCurrentUser().getId());
                }else{
                    fileIdStack.add(fileId);
                }
            }
            //刷新当前目录,取出栈顶元素
            else if(type==TYPE_REFRESH){
                if (!fileIdStack.empty()){
                    fileId=fileIdStack.peek();
                }
                else {
                    fileId=String.valueOf(AppApplication.getCurrentUser().getId());
                }
            }
            fileModel.searchSubfile(fileId, new CloudListener<List<Files>>() {
                @Override
                public void done(List<Files> object, CloudExecption e) {
                    if(e==null){
                        //将查询到的数据入栈，返回上个目录时直接拿到数据
                        if(type==TYPE_REFRESH){
                            subFileStack.pop();
                        }
                        subFileStack.push(object);
                        getView().setRecycleViewData(object);
                    }
                }
            });
        }
    }


    /*
    * 上传文件
    * */
    public void uploadFile(String[] paths){
        //获取当前用户id
        String userId=String.valueOf(AppApplication.getCurrentUser().getId());
        //获取当前所在文件夹id
        String folderId;
        if(!fileIdStack.empty()) {
            folderId = fileIdStack.peek();
        }else{
            folderId=userId;
        }

        Map<String,String> options=new HashMap<>();
        Map<String, RequestBody> params=new HashMap<>();
        options.put("userId",userId);
        options.put("folderId",folderId);
        for(int i=0;i<paths.length;i++){
            File file=new File(paths[i]);
            params.put("file"+i+"\"; filename=\""+file.getName(),RequestBody.create(MediaType.parse("multipart/form-data"), file));
        }
        fileModel.uploadFile(options, params, new CloudListener<Files>() {
            @Override
            public void done(Files object, CloudExecption e) {
                if(e==null){
                    getView().uploadResult(true);
                }else{
                    getView().uploadResult(false);
                }
            }
        });
    }

}
